package com.xidian.zhiancloud.service.luling.TrainingRiskStatusPart;

import com.xidian.zhiancloud.dao.TrainingRiskStatusMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.TrainingRiskStatus;
import com.xidian.zhiancloud.model.TrainingRiskStatusExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.xidian.zhiancloud.util.tools.ObjectUtil.isEmpty;

@Service
public class TrainingRiskStatusService {
    @Autowired
    private TrainingRiskStatusMapper      trainingRiskStatusMapper;

        @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
        public void add(TrainingRiskStatus trainingRiskStatus) {
            if(!isEmpty(trainingRiskStatus)){
                trainingRiskStatusMapper.insert(trainingRiskStatus);
            }else{
                throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
            }
        }
    //通过状态查询
    public List<TrainingRiskStatus> getById(String state) {
        TrainingRiskStatusExample trainingRiskStatusExample = new TrainingRiskStatusExample();
        TrainingRiskStatusExample.Criteria criteria = trainingRiskStatusExample.createCriteria();
        criteria.andStateEqualTo(state);
        List<TrainingRiskStatus> trainingRiskStatusList = trainingRiskStatusMapper.selectByExample(trainingRiskStatusExample);
        if (trainingRiskStatusList == null) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        return trainingRiskStatusList;
    }

    public Object getList(String state, Date startTimeTrain, String lecturer, String locale, String riskProject, String key, int current, int size) {
        TrainingRiskStatusExample trainingRiskStatusExample = new TrainingRiskStatusExample();
        TrainingRiskStatusExample.Criteria criteria1 = trainingRiskStatusExample.createCriteria();
        TrainingRiskStatusExample.Criteria criteria2 = trainingRiskStatusExample.createCriteria();
        TrainingRiskStatusExample.Criteria criteria3 = trainingRiskStatusExample.createCriteria();
        criteria1.andLecturerEqualTo(lecturer);
        criteria2.andLecturerEqualTo(lecturer);
        criteria3.andLecturerEqualTo(lecturer);
        if (state != null) {
            criteria1.andStateEqualTo(state);
            criteria2.andStateEqualTo(state);
            criteria3.andStateEqualTo(state);
        }
        if (startTimeTrain != null) {
            criteria1.andStartTimeTrainEqualTo(startTimeTrain);
            criteria2.andStartTimeTrainEqualTo(startTimeTrain);
            criteria3.andStartTimeTrainEqualTo(startTimeTrain);
        }
        if (locale != null) {
            criteria1.andLocaleEqualTo(locale);
            criteria2.andLocaleEqualTo(locale);
            criteria3.andLocaleEqualTo(locale);
        }
        if (riskProject != null) {
            criteria1.andRiskProjectEqualTo(riskProject);
            criteria2.andRiskProjectEqualTo(riskProject);
            criteria3.andRiskProjectEqualTo(riskProject);
        }
        if (key!= null && !"".equals(key)) {
            criteria1.andRiskProjectEqualTo('%' + key + '%');
            criteria2.andLocaleEqualTo('%' + key + '%');
            criteria3.andLecturerEqualTo('%' + key + '%');
            trainingRiskStatusExample.or(criteria2);
            trainingRiskStatusExample.or(criteria3);
        }
        return trainingRiskStatusMapper.selectByExampleWithRowbounds(trainingRiskStatusExample, new RowBounds(current, size));
    }
        }


